Test Setup Failed
Push — master ( 51607c...6b8ca8 )
by Paul
03:43
created

GLSR.search.init   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 9.4285
1
GLSR.search = function( el, options )
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
2
{
3
	this.el = Object.prototype.toString.call( el ) === '[object String]' ? x( el ) : el;
4
	this.options = options;
5
	this.searchTerm = null;
6
	this.init();
7
};
8
9
GLSR.search.prototype =
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
{
11
	defaults: {
12
		action: null,
13
		exclude: [],
14
		onInit: null,
15
		onResultClick: null,
16
		results: {},
17
		selected: -1,
18
		selectedClass: 'glsr-selected-result',
19
		selectorEntries: '.glsr-strings-table tbody',
20
		selectorResults: '.glsr-search-results',
21
		selectorSearch: '.glsr-search-input',
22
		// entriesEl
23
		// resultsEl
24
		// searchEl
25
	},
26
27
	/** @return void */
28
	init: function()
29
	{
30
		this.options = x.extend( {}, this.defaults, this.options );
0 ignored issues
show
Bug introduced by
The variable x seems to be never declared. If this is a global, consider adding a /** global: x */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
31
		if( !this.el.length )return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
32
		this.options.entriesEl = this.el.parent().find( this.options.selectorEntries );
33
		this.options.resultsEl = this.el.find( this.options.selectorResults );
34
		this.options.searchEl = this.el.find( this.options.selectorSearch );
35
		this.options.searchEl.attr( 'aria-describedby', 'live-search-desc' );
36
		if( typeof this.options.onInit === 'function' ) {
37
			this.options.onInit.call( this );
38
		}
39
		this.initEvents();
40
	},
41
42
	/** @return void */
43
	initEvents: function()
44
	{
45
		this.options.searchEl.on( 'input', _.debounce( this.onSearchInput.bind( this ), 500 ));
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
46
		this.options.searchEl.on( 'keyup', this.onSearchKeyup.bind( this ));
47
		x( document ).on( 'click', this.onDocumentClick.bind( this ));
48
		x( document ).on( 'keydown', this.onDocumentKeydown.bind( this ));
49
	},
50
51
	/** @return void */
52
	abort: function()
53
	{
54
		if( 'undefined' === typeof this.searchRequest )return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
55
		this.searchRequest.abort();
56
	},
57
58
	/** @return void */
59
	clearResults: function()
60
	{
61
		this.abort();
62
		this.options.resultsEl.empty();
63
		this.el.removeClass( 'is-active' );
64
		x( 'body' ).removeClass( 'glsr-focus' );
65
	},
66
67
	/** @return void */
68
	displayResults: function( items )
69
	{
70
		x( 'body' ).addClass( 'glsr-focus' );
71
		this.options.resultsEl.append( items );
72
		this.options.resultsEl.children( 'span' ).on( 'click', this.onResultClick.bind( this ));
73
	},
74
75
	/** @return void */
76
	navigateResults: function( diff )
77
	{
78
		this.options.selected += diff;
79
		this.options.results.removeClass( this.options.selectedClass );
80
		if( this.options.selected < 0 ) {
81
			// reached the start (should now allow keydown scroll)
82
			this.options.selected = -1;
83
			this.options.searchEl.focus();
84
		}
85
		if( this.options.selected >= this.options.results.length ) {
86
			// reached the end (should now allow keydown scroll)
87
			this.options.selected = this.options.results.length - 1;
88
		}
89
		if( this.options.selected >= 0 ) {
90
			this.options.results.eq( this.options.selected )
91
				.addClass( this.options.selectedClass )
92
				.focus();
93
		}
94
	},
95
96
	/** @return void */
97
	onDocumentClick: function( ev )
98
	{
99
		if( x( ev.target ).find( this.el ).length && x( 'body' ).hasClass( 'glsr-focus' )) {
100
			this.clearResults();
101
		}
102
	},
103
104
	/** @return void */
105
	onDocumentKeydown: function( ev )
106
	{
107
		if( !this.options.results )return;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
108
		if( GLSR.keys.ESC === ev.which ) {
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
109
			this.clearResults();
110
		}
111
		if( GLSR.keys.ENTER === ev.which || GLSR.keys.SPACE === ev.which ) {
112
			var selected = this.options.resultsEl.find( '.' + this.options.selectedClass );
113
			if( selected ) {
114
				selected.trigger( 'click' );
115
			}
116
		}
117
		if( GLSR.keys.UP === ev.which ) {
118
			ev.preventDefault();
119
			this.navigateResults(-1);
120
		}
121
		if( GLSR.keys.DOWN === ev.which ) {
122
			ev.preventDefault();
123
			this.navigateResults(1);
124
		}
125
	},
126
127
	/** @return void */
128
	onResultClick: function( ev )
129
	{
130
		ev.preventDefault();
131
		if( typeof this.options.onResultClick === 'function' ) {
132
			this.options.onResultClick.call( this, ev );
133
		}
134
		this.clearResults();
135
	},
136
137
	/** @return void */
138
	onSearchInput: function( ev )
139
	{
140
		this.abort();
141
		if( this.searchTerm === ev.target.value && this.options.results.length ) {
142
			return this.displayResults( this.options.results );
143
		}
144
		this.options.resultsEl.empty();
145
		this.options.selected = -1;
146
		this.searchTerm = ev.target.value;
147
		if( this.searchTerm === '' ) {
148
			return this.reset();
149
		}
150
		this.el.addClass( 'is-active' );
151
		this.searchRequest = wp.ajax.post( site_reviews.action, {
0 ignored issues
show
Bug introduced by
The variable site_reviews seems to be never declared. If this is a global, consider adding a /** global: site_reviews */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable wp seems to be never declared. If this is a global, consider adding a /** global: wp */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
152
			request: {
153
				action: this.options.action,
154
				exclude: this.options.exclude,
155
				nonce: this.el.find( '#_wpnonce' ).val(),
156
				search: this.searchTerm,
157
			},
158
		}).done( function( response ) {
159
			this.el.removeClass( 'is-active' );
160
			this.displayResults( response.items ? response.items : response.empty );
161
			this.options.results = this.options.resultsEl.children();
162
			delete this.searchRequest;
163
		}.bind( this ));
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
164
	},
165
166
	/** @return void */
167
	onSearchKeyup: function( ev )
168
	{
169
		if( GLSR.keys.ESC === ev.which ) {
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
170
			this.reset();
171
		}
172
		if( GLSR.keys.ENTER === ev.which ) {
173
			this.onSearchInput( ev );
174
		}
175
	},
176
177
	/** @return void */
178
	reset: function()
179
	{
180
		this.clearResults();
181
		this.options.results = {};
182
		this.options.searchEl.val( '' );
183
	},
184
};
185
186
// Manage Entries
187
188
GLSR.search.prototype.deleteEntry = function( index )
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
189
{
190
	var row = this.options.entriesEl.children( 'tr' ).eq( index );
191
	var search = this;
192
	row.find( 'td' ).css({ backgroundColor:'#faafaa' });
193
	row.fadeOut( 350, function() {
194
		x( this ).remove();
195
		search.options.results = {};
196
		search.reindexRows();
197
		search.setVisibility();
198
	});
199
};
200
201
GLSR.search.prototype.makeSortable = function()
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
202
{
203
	this.options.entriesEl.on( 'click', 'a.delete', this.onEntryDelete.bind( this ));
204
	this.options.entriesEl.sortable({
205
		items: 'tr',
206
		tolerance: 'pointer',
207
		start: function( ev, ui ) {
208
			ui.placeholder.height( ui.helper[0].scrollHeight );
209
		},
210
		sort: function( ev, ui ) {
211
			var top = ev.pageY - x( this ).offsetParent().offset().top - ( ui.helper.outerHeight( true ) / 2 );
212
			ui.helper.css({
213
				top: top + 'px',
214
			});
215
		},
216
	});
217
};
218
219
GLSR.search.prototype.onEntryDelete = function( ev )
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
220
{
221
	ev.preventDefault();
222
	this.deleteEntry( x( ev.target ).closest( 'tr' ).index() );
223
};
224
225
GLSR.search.prototype.onUnassign = function( ev )
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
226
{
227
	ev.preventDefault();
228
	var assigned = this.el.find( '.description' );
229
	this.el.find( 'input#assigned_to' ).val( '' );
230
	assigned.find( 'a' ).css({ color:'#c00' });
231
	assigned.fadeOut( 'fast', function() {
232
		x( this ).html( '' ).show();
233
	});
234
};
235
236
GLSR.search.prototype.reindexRows = function()
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
237
{
238
	var search = this;
239
	this.options.exclude = [];
240
	this.options.entriesEl.children( 'tr' ).each( function( index ) {
241
		x( this ).find( '.glsr-string-td2' ).children().filter( ':input' ).each( function() {
242
			var input = x( this );
243
			var name = input.attr( 'name' ).replace( /\[\d+\]/i, '[' + index + ']' );
244
			input.attr( 'name', name );
245
			if( input.is( '[data-id]' )) {
246
				search.options.exclude.push({ id: input.val() });
247
			}
248
		});
249
	});
250
};
251
252
GLSR.search.prototype.setVisibility = function()
0 ignored issues
show
Bug introduced by
The variable GLSR seems to be never declared. If this is a global, consider adding a /** global: GLSR */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
253
{
254
	var action = this.options.entriesEl.children().length > 0 ? 'remove' : 'add';
255
	this.options.entriesEl.parent()[action + 'Class']( 'glsr-hidden' );
256
};
257